package com.ujiuye.web.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ujiuye.pojo.Book;
import com.ujiuye.pojo.ResultVo;
import com.ujiuye.service.BookService;
import com.ujiuye.utils.PageTool;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/book")
public class BookServlet extends BaseServlet {
    BookService bs = new BookService();
    ResultVo vo = new ResultVo();
    //查看所有书籍信息
    public void lookAllBooks(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询总条数
        //int totalCount = bs.lookCounts();
        //分页查询
        //String currentPage = req.getParameter("currentPage");
        //PageTool pageTool = new PageTool(currentPage, totalCount);
        String num = req.getParameter("num");
        List<Book> blist = bs.lookAllBooks(num);
        if (blist != null && blist.size() > 0) {
            vo = new ResultVo(1, "查询成功", blist);
            String s = new ObjectMapper().writeValueAsString(vo);
            //System.out.println(s);
            resp.getWriter().print(s);
        } else {
            vo = new ResultVo(0, "查询失败", blist);
        }
    }

    //添加图书信息
    public void addBook(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String[]> parameterMap = req.getParameterMap();
        Book book = new Book();
        BeanUtils.populate(book, parameterMap);
        boolean flag = bs.addBook(book);
        if (flag) {
            vo = new ResultVo(1, "添加成功", flag);
            String s = new ObjectMapper().writeValueAsString(vo);
            resp.getWriter().print(s);
        } else {
            vo = new ResultVo(0, "添加失败", flag);
        }
    }
    //通过bid查询图书信息

    public void lookBookByBid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bid = req.getParameter("bid");
        Book book = bs.lookBookByBid(bid);
        if (book != null) {
            vo = new ResultVo(1,"查询成功",book);
            String s = new ObjectMapper().writeValueAsString(vo);
            resp.getWriter().print(s);
        }else {
            vo = new ResultVo(0,"查询失败",book);
            String s = new ObjectMapper().writeValueAsString(vo);
            resp.getWriter().print(s);
        }
    }
    //修改图书信息
    public void upDateBook(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        Map<String, String[]> map = req.getParameterMap();
        Book book = new Book();
        BeanUtils.populate(book,map);
        boolean flag = bs.upDateBook(book);
        if (flag){
            vo= new ResultVo(1,"修改成功",flag);
            String s = new ObjectMapper().writeValueAsString(vo);
            resp.getWriter().print(s);
        }else {
            vo = new ResultVo(0,"修改失败",flag);
            String s = new ObjectMapper().writeValueAsString(vo);
            resp.getWriter().print(s);
        }
    }
    //删除图书信息
    public void deleteBookByBid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String bid = req.getParameter("bid");
        boolean flag = bs.deleteBookByBid(bid);
        if (flag){
            vo=new ResultVo(1,"删除成功",flag);
            String s = new ObjectMapper().writeValueAsString(vo);
            resp.getWriter().print(s);

        }else {
            vo = new ResultVo(0,"删除失败",flag);
            String s = new ObjectMapper().writeValueAsString(vo);
            resp.getWriter().print(s);
        }
    }
}
